/*************************************************
	作者: Plane
	邮箱: 1785275942@qq.com
	日期: 2021/03/05 1:50
	功能: 计时任务抽象基类 (优化版)

    //=================*=================\\
           教学官网：www.qiqiker.com
           关注微信服务号: qiqikertuts
           关注微信公众号: PlaneZhong
               ~~获取更多教学资讯~~
    \\=================*=================//
*************************************************/

using System;
using System.Collections.Generic;

namespace PETimer {
    /// <summary>
    /// 任务状态枚举
    /// </summary>
    public enum TaskStatus {
        Running,    // 运行中
        Paused,     // 已暂停
        Completed,  // 已完成
        Cancelled   // 已取消
    }

    /// <summary>
    /// 任务信息
    /// </summary>
    public struct TaskInfo {
        public int tid;
        public uint delay;
        public int remainingCount;
        public TaskStatus status;
    }

    public abstract class PETimer {
        public Action<string> LogFunc;
        public Action<string> WarnFunc;
        public Action<string> ErrorFunc;

        /// <summary>
        /// 创建定时任务
        /// </summary>
        /// <param name="delay">定时任务时间（单位：毫秒或帧数）</param>
        /// <param name="taskCB">定时任务回调</param>
        /// <param name="cancelCB">取消任务回调</param>
        /// <param name="count">任务重复计数（0表示无限循环）</param>
        /// <returns>当前计时器唯一任务ID，失败返回-1</returns>
        public abstract int AddTask(uint delay, Action<int> taskCB, Action<int> cancelCB, int count = 1);

        /// <summary>
        /// 删除定时任务
        /// </summary>
        /// <param name="tid">定时任务ID</param>
        /// <returns>删除操作结果</returns>
        public abstract bool DeleteTask(int tid);

        /// <summary>
        /// 重置定时器（清空所有任务）
        /// </summary>
        public abstract void Reset();

        /// <summary>
        /// 暂停指定任务（如果支持）
        /// </summary>
        /// <param name="tid">任务ID</param>
        /// <returns>操作是否成功</returns>
        public virtual bool PauseTask(int tid) {
            WarnFunc?.Invoke("PauseTask is not supported by this timer implementation.");
            return false;
        }

        /// <summary>
        /// 恢复指定任务（如果支持）
        /// </summary>
        /// <param name="tid">任务ID</param>
        /// <returns>操作是否成功</returns>
        public virtual bool ResumeTask(int tid) {
            WarnFunc?.Invoke("ResumeTask is not supported by this timer implementation.");
            return false;
        }

        /// <summary>
        /// 查询任务是否存在
        /// </summary>
        /// <param name="tid">任务ID</param>
        /// <returns>任务是否存在</returns>
        public virtual bool HasTask(int tid) {
            return false;
        }

        /// <summary>
        /// 获取任务信息
        /// </summary>
        /// <param name="tid">任务ID</param>
        /// <returns>任务信息，如果不存在返回null</returns>
        public virtual TaskInfo? GetTaskInfo(int tid) {
            return null;
        }

        /// <summary>
        /// 获取所有任务ID列表
        /// </summary>
        /// <returns>任务ID列表</returns>
        public virtual List<int> GetAllTaskIds() {
            return new List<int>();
        }

        /// <summary>
        /// 批量删除任务
        /// </summary>
        /// <param name="tids">任务ID列表</param>
        /// <returns>成功删除的任务数量</returns>
        public virtual int DeleteTasks(List<int> tids) {
            int count = 0;
            foreach(var tid in tids) {
                if(DeleteTask(tid)) {
                    count++;
                }
            }
            return count;
        }

        /// <summary>
        /// 获取当前任务总数
        /// </summary>
        /// <returns>任务总数</returns>
        public virtual int GetTaskCount() {
            return 0;
        }

        protected int tid = 0;
        protected abstract int GenerateTid();
    }
}

